/*
 * FileName：SysUserServiceImpl.java
 * <p>
 * Copyright (c) 2017-2020 <a href="https://www.smartwx.info">hermit(1154808491@qq.com)</a>.
 * <p>
 * Licensed under the GNU General Public License, Version 3 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/gpl-3.0.html
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package com.smart.sys.service.impl;

import javax.annotation.Resource;

import com.smart.common.Constants;
import com.smart.common.util.MD5Util;
import com.smart.common.util.SessionUtil;
import com.smart.common.util.StringUtil;
import com.smart.common.util.Util;
import com.smart.sys.dao.SysDeptMapper;
import com.smart.sys.dao.SysUserRoleMapper;
import com.smart.sys.entity.SysDept;
import org.hibernate.validator.constraints.ISBN;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

import com.smart.sys.dao.SysUserMapper;
import com.smart.sys.entity.SysUser;
import com.smart.sys.service.ISysUserService;

/**
 * 用户信息
 *
 * @author hermit
 * @version 2.0
 * @date 2018-12-30 22:26:32
 */
@Service
@Transactional
public class SysUserServiceImpl implements ISysUserService {

    @Resource
    private SysUserMapper sysUserMapper;

    @Resource
    private SysUserRoleMapper sysUserRoleMapper;

    @Override
    public SysUser queryObject(SysUser sysUser) throws Exception {
        return sysUserMapper.queryObject(sysUser);
    }

    @Override
    public List<SysUser> queryList(SysUser sysUser) throws Exception {
        return sysUserMapper.queryList(sysUser);
    }

    @Override
    public List<SysUser> queryListByPage(SysUser sysUser) throws Exception {
        return sysUserMapper.queryListByPage(sysUser);
    }

    @Override
    public Integer queryCount(SysUser sysUser) throws Exception {
        Integer ct = sysUserMapper.queryCount(sysUser);
        return ct == null ? 0 : ct;
    }

    @Override
    public Integer insert(SysUser sysUser) throws Exception {
        sysUser.setUserId(Util.getUUID());
        sysUser.setCreateTime(new Date());
        sysUser.setUpdateTime(new Date());
        sysUser.setCreateUser(SessionUtil.getUserId());
        sysUser.setUpdateUser(SessionUtil.getUserId());
        sysUser.setPassword(MD5Util.getMD5Code(sysUser.getPassword()));
        sysUser.setDelFlag(Constants.DEL_FLAG_NORMAL);
        sysUser.setSystemFlag(Constants.SYSTEM_FLAG_NO);
        if (StringUtil.isBlank(sysUser.getUserType())) {
            sysUser.setUserType(Constants.SYSTEM_USER_TYPE);
        }
        sysUser.setStatus(Constants.STATE_ENABLE);
        if (StringUtil.isBlank(sysUser.getSex())) {
            sysUser.setSex(Constants.SEX_NO);
        }
        //默认头像
        sysUser.setAvatar("//q.qlogo.cn/g?b=qq&nk=1154808491&s=100&t=1538227404992");
        return sysUserMapper.insert(sysUser);
    }

    @Override
    public Integer update(SysUser sysUser) throws Exception {
        sysUser.setUpdateTime(new Date());
        sysUser.setUpdateUser(SessionUtil.getUserId());
        return sysUserMapper.update(sysUser);
    }

    @Override
    public Integer delete(SysUser sysUser) throws Exception {
        return sysUserMapper.delete(sysUser);
    }

    @Override
    public Integer deleteBatch(List<String> userIds) throws Exception {
        sysUserRoleMapper.deleteBatchByUser(userIds);
        return sysUserMapper.deleteBatch(userIds);
    }

    @Override
    public Integer resetPassword(List<String> ids) throws Exception {
        return sysUserMapper.resetPassword(ids, MD5Util.getMD5Code(Constants.USER_DEFAULT_PASSWORD));
    }
}
